package com.freshcity.component.container
{
	import fl.controls.LabelButton;
	import fl.core.InvalidationType;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	/**
	 * 选项卡面板
	 * @author pw
	 * 
	 */
	public class TabPanel extends TabBar{
		private static const TABBAR_MIN_WIDTH:int = 10;
		private static const TABBAR_MIN_HEIGHT:int = 10;
		/** 组件的背景 */
		protected var background:DisplayObject;
		/** 子项的容器 */
		protected var container:DisplayObject;
		/** 已经载入的子项 */
		protected var _contains:Array;
		/** 显示内容对象的名字 */
		protected var _contentNames:Array;
		/** 对应于contentPadding样式 */
		protected var _contentPadding:Number = 1;
		/** 按钮占的宽 */
		protected var _tabBarWidth:Number = 120;
		/** 按钮占的高 */
		protected var _tabBarHeight:Number = 30;
		
		protected var _tabBarAlignV:String = LayoutAlign.TOP;
		protected var _tabBarAlignH:String = LayoutAlign.LEFT;
		private static var defaultStyles:Object = {
			bgskin:"TabPanel_bgskin"
		}
		public function TabPanel()
		{
			super();
			_contains = [];
			_contentNames = [];
		}
		/** 利用mergeStyles()把两个类的样式合并 */
		public static function getStyleDefinition():Object {
			return mergeStyles(defaultStyles, TabBar.getStyleDefinition());
		}
		/** 重写初始化显示对象 */
		override protected function configUI():void{
			super.configUI();
			container = new Sprite as DisplayObject;
			addChild(container);
		}
		/** 重写重绘方法（核心） */
		override protected function draw():void{
			if (isInvalid(InvalidationType.STYLES)){
				drawBackground();
			}
			if (InvalidationType.SELECTED){
				drawContent();
				doLayout();
			}
			if (isInvalid(InvalidationType.DATA)){
				_contains = [];
				drawContent();
			}
			if (isInvalid(InvalidationType.SIZE, InvalidationType.DATA)){
				doLayout();
			}
			super.draw();
		}
		protected function drawBackground():void {
			
			var bg:DisplayObject = background;
			
			background = getDisplayObjectInstance(getStyleValue("bgskin"));
			background.width = width;
			background.height = height;
			addChildAt(background, 0);
			
			if (bg != null && bg != background) {
				removeChild(bg);
			}
		}
		protected function drawContent():void {
			if(_contentNames[_selectedIndex] == null)return;
			
			if(_contains[_selectedIndex] == null){
				var disObContent:DisplayObject = getDisplayObjectInstance(_contentNames[_selectedIndex]);
				if(!disObContent)return;
				_contains[_selectedIndex] = disObContent;
			}
			
			removeChild(container);
			container = _contains[_selectedIndex];
			addChild(container)
		}
		override protected function doLayout():void {
			background.width = width;
			background.height = height;
			var len:uint = arrBtns.length;
			var btn:LabelButton;
			var btnWidth:Number;
			var btnHeight:Number;
			if(_direction == LayoutDirection.HORIZONTAL){
				btnWidth = (_tabBarWidth - _btnSpace * (len - 1)) / len;
				btnHeight = _tabBarHeight;
				for(var i:uint = 0; i < len; i++){
					btn = arrBtns[i];
					doBtnLayout(btn, i, btnWidth, btnHeight);
					btn.setSize(btnWidth, btnHeight);
					
					btn.drawNow();
				}
				if(_tabBarAlignV == LayoutAlign.BOTTOM){
					container.y = contentPadding;
				}else{
					container.y = btnHeight + contentPadding;
				}
				container.x = contentPadding;
				
				container.scrollRect = new Rectangle(0, 0, width - contentPadding * 2, height - btnHeight - contentPadding * 2);
			}else{
				btnWidth = _tabBarWidth;
				btnHeight = (_tabBarHeight - _btnSpace * (len - 1)) / len;
				for(var j:uint = 0; j < len; j++){
					btn = arrBtns[j];
					doBtnLayout(btn, j, btnWidth, btnHeight);
					btn.setSize(btnWidth, btnHeight);
					
					btn.drawNow();
				}
				if(_tabBarAlignH == LayoutAlign.RIGHT){
					container.x = contentPadding;
				}else{
					container.x = btnWidth + contentPadding;
				}
				container.y = contentPadding;
				
				container.scrollRect = new Rectangle(0, 0, width - btnWidth - contentPadding * 2, height - contentPadding * 2);
			}
		}
		/**
		 * 选项卡按钮的布局
		 * @param btn 按钮
		 * @param index 所在索引
		 * @param w 按钮宽
		 * @param h 按钮高
		 * 
		 */		
		protected function doBtnLayout(btn:LabelButton, index:uint, w:Number, h:Number):void{
			var len:uint = arrBtns.length;
			var leaning:Number;
			if(_direction == LayoutDirection.HORIZONTAL){
				switch(_tabBarAlignH){
					case LayoutAlign.LEFT:
						btn.x = (w + _btnSpace) * index;
						break;
					case LayoutAlign.CENTER:
						leaning = (width - ((w + _btnSpace) * (len - 1) + w)) / 2;
						btn.x = (w + _btnSpace) * index + leaning;
						break;
					case LayoutAlign.RIGHT:
						leaning = width - ((w + _btnSpace) * (len - 1) + w);
						btn.x = (w + _btnSpace) * index + leaning;
						break;
					case LayoutAlign.JUSTIFY:
						btn.x = index * (width - w) / (len - 1);
						break;
				}
				if(_tabBarAlignV == LayoutAlign.BOTTOM){
					btn.y = height - h;
				}
				
			}else{
				switch(_tabBarAlignV){
					case LayoutAlign.TOP:
						btn.y = (h + _btnSpace) * index;
						break;
					case LayoutAlign.MIDDLE:
						leaning = (height - ((h + _btnSpace) * (len - 1) + h)) / 2;
						btn.y = (h + _btnSpace) * index + leaning;
						break;
					case LayoutAlign.BOTTOM:
						leaning = height - ((h + _btnSpace) * (len - 1) + h);
						btn.y = (h + _btnSpace) * index + leaning;
						break;
					case LayoutAlign.JUSTIFY:
						btn.y = index * (height - h) / (len - 1);
						break;
				}
				if(_tabBarAlignH == LayoutAlign.RIGHT){
					btn.x = width - w;
				}
			}
		}
		public function get content():DisplayObject{
			return container;
		}
		/** 按钮占的宽 */		
		[Inspectable(defaultValue = 120)]
		public function get tabBarWidth():Number{
			return _tabBarWidth;
		}
		public function set tabBarWidth(v:Number):void{
			v = v < TabPanel.TABBAR_MIN_HEIGHT ? TabPanel.TABBAR_MIN_HEIGHT : v;
			if(_tabBarWidth == v)return;
			_tabBarWidth = v;
			invalidate(InvalidationType.SIZE);
		}
		/** 按钮占的高 */		
		[Inspectable(defaultValue = 30)]
		public function get tabBarHeight():Number{
			return _tabBarHeight;
		}
		public function set tabBarHeight(v:Number):void{
			v = v < TabPanel.TABBAR_MIN_HEIGHT ? TabPanel.TABBAR_MIN_HEIGHT : v;
			if(_tabBarHeight == v)return;
			_tabBarHeight = v;
			invalidate(InvalidationType.SIZE);
		}
		[Inspectable(defaultValue = 1)]
		public function get contentPadding():Number{
			return _contentPadding;
		}
		public function set contentPadding(v:Number):void{
			if(_contentPadding == v)return;
			_contentPadding = v;
			invalidate(InvalidationType.SIZE);
		}
		[Inspectable()]
		public function get contentNames():Array{
			return _contentNames;
		}
		public function set contentNames(arr:Array):void{
			_contentNames = arr;
			invalidate(InvalidationType.DATA);
		}
		[Inspectable(enumeration="left,center,right,justify", defaultValue="left")]
		public function get tabBarAlignH():String { 
			return _tabBarAlignH;
		}
		public function set tabBarAlignH(v:String):void {
			if(_tabBarAlignH == v)return;
			_tabBarAlignH = v;
			invalidate(InvalidationType.SIZE);
		}
		[Inspectable(enumeration="top,bottom,middle,justify", defaultValue="top")]
		public function get tabBarAlignV():String { 
			return _tabBarAlignV;
		}
		public function set tabBarAlignV(v:String):void {
			if(_tabBarAlignV == v)return;
			_tabBarAlignV = v;
			invalidate(InvalidationType.SIZE);
		}
	}
}